package runInfo;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class Plot {
    public static ArrayList<Double> tempTCArr 	= new ArrayList<Double>();  
    public static ArrayList<Double> tempTFArr 	= new ArrayList<Double>();  
    public static ArrayList<Double> tempACArr 	= new ArrayList<Double>();  
    public static ArrayList<Double> tempAFArr 	= new ArrayList<Double>();    
    public static ArrayList<String> dateTime 	= new ArrayList<String>();  
    public static ArrayList<Double> timeS	 	= new ArrayList<Double>();  
    public static ArrayList<Double> boilerTemp 	= new ArrayList<Double>();  
    public static ArrayList<Double> distTemp 	= new ArrayList<Double>();  
    public static ArrayList<Double> abv 		= new ArrayList<Double>();  
    public static ArrayList<Double> abvUncorr 	= new ArrayList<Double>();

    public static ArrayList<Integer> bottle 	= new ArrayList<Integer>(); 
    
	// Update in DAT files =======================
	private static String ReadinData() {
		System.out.println("Reading in DAT file....");									// import data from file (DatFile.Index), line by line (i), into arrays (j)
		BufferedReader readbuffer = null;
		String filePathName = "C:\\WhiskeyData\\output.txt";
		
		try {
			readbuffer = new BufferedReader(new FileReader(filePathName));
//					"" + DatFile.Path[DatFile.Index] + "\\" + DatFile.Name[DatFile.Index]));
		} catch (FileNotFoundException e1) {
			System.out.println("File not found:" + filePathName); 
			e1.printStackTrace();
		}
		String strRead;
//		String FileData = currentDir; //not used...
		int i = 0; 						// where we are in the file
		int j = 0; 						// where we are in the data
		int k = 0; 						// index to import next set of data aquisition initialization
		int mult = 0; 					// num of restarts
		int multFlag = 0; 				//was there a restart in the last sequence?
		double addtime = 0.0;	
		double time1 = 0.0;
		double time2 = 0.0;
		double time3 = 0.0;
		double time4 = 0.0;
		double timediff = 0.0;
		int m = 0;
		boolean UserTimeAdd = false;
		mult = multFlag;
		String tempstr1;
		String tempstr2;
		
		int sparse = 1;
		if(Data.numOfWrites > 1500){
			sparse = Data.numOfWrites / 1500;
		}
		try {
			while ((strRead = readbuffer.readLine()) != null) {
				//System.out.println("i = " + i);
				String splitarray[] = strRead.split("\t");
				if(k==0){		// header
					// datetime	min	columnC	columnF	ambientC	ambientF	bottleNumber	correctedAbv	uncorrectedAbv	distillateTempC	boilerTempC
					//	0		1	2		3		4			5			6				7				8				9				10
				}
				if(k>0){		// read in data
					if(splitarray.length==11 && (sparse == 1 || (j % sparse) == 1)){ 						// check to make sure there is data there		
						dateTime.add(splitarray[0].trim());
						timeS.add(Double.parseDouble(splitarray[1].trim()));
						tempTCArr.add(Double.parseDouble(splitarray[2].trim()));
						tempTFArr.add(Double.parseDouble(splitarray[3].trim()));
						tempACArr.add(Double.parseDouble(splitarray[4].trim()));
						tempAFArr.add(Double.parseDouble(splitarray[5].trim()));
						bottle.add(Integer.parseInt(splitarray[6].trim()));
						abv.add(Double.parseDouble(splitarray[7].trim()));
						abvUncorr.add(Double.parseDouble(splitarray[8].trim()));
						distTemp.add(Double.parseDouble(splitarray[9].trim()));
						boilerTemp.add(Double.parseDouble(splitarray[10].trim()));								
						j++;
					}
				}
				k=k+1;
				i=i+1;			
			}
		} catch (IOException e1) {e1.printStackTrace();}											
		try {readbuffer.close();} 						//Close the stream to free memory resources on the host computer:
		catch (IOException e1) {e1.printStackTrace();}
		addtime = 0;
		return null;
	}
	
	static JFreeChart CreateChart() {
		ReadinData();
		XYSeries series1 = new XYSeries("Column Temperature");						// declare new series to add to the figure
		XYSeries series2 = new XYSeries("Bottle Number");
		XYSeries series3 = new XYSeries("Boiler Temperature");
		XYSeries series4 = new XYSeries("set3 - L");		

		for(int i=0;i<tempTCArr.size();i++){											// load the data into the series
			series1.add(timeS.get(i),tempTCArr.get(i));	
			series2.add(timeS.get(i),bottle.get(i));
			series3.add(timeS.get(i),boilerTemp.get(i));	
		}
		
		XYSeriesCollection dataset = new XYSeriesCollection();						// First Axis data
		dataset.addSeries(series1);
		dataset.addSeries(series3);
		
		JFreeChart chart = ChartFactory.createXYLineChart( 							// create the cBar... JFreeChart cBar = ChartFactory.createXYAreaChart(
				"Distilling Data", 													// cBar title
				"time [min]", 														// x axis label
				"Temperature [C]", 													// y axis label
				dataset, 															// data
				PlotOrientation.VERTICAL,
				true, 																// include legend
				true, 																// tooltips
				false 																// urls
				);
		
		chart.setBackgroundPaint(Color.white); 										// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
		XYPlot plot = (XYPlot) chart.getPlot(); 									// get a reference to the plot for further customization...
		plot.setBackgroundPaint(Color.white);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainGridlinePaint(Color.lightGray);
		plot.setRangeGridlinePaint(Color.lightGray);
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();					// change the auto tick unit selection to integer units only...
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());		// OPTIONAL CUSTOMISATION COMPLETED.
		
		XYItemRenderer r1 = plot.getRenderer();										// set characteristics for data
		if (r1 instanceof XYLineAndShapeRenderer) {
			XYLineAndShapeRenderer renderer1 = (XYLineAndShapeRenderer) r1;
//			renderer1.setBaseShapesVisible(true);										
//			renderer1.setShapesVisible(true);											
//			renderer1.setShapesFilled(true);
			renderer1.setSeriesPaint(0, Color.green);
//			renderer1.setSeriesPaint(1, Color.black);
//			renderer1.setSeriesPaint(2, Color.blue);
			plot.setRenderer(0, renderer1);
		}
		
		//--------------------------------------------
		XYSeriesCollection dataset2 = new XYSeriesCollection();						// Axis 2 - dataset
		dataset2.addSeries(series2);
		
        NumberAxis axis2 = new NumberAxis("Bottle Number");
        axis2.setFixedDimension(10.0);
        axis2.setAutoRangeIncludesZero(false);
        axis2.setLabelPaint(Color.blue);
        axis2.setTickLabelPaint(Color.blue);
        plot.setRangeAxis(1, axis2);
        plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
        
        plot.setDataset(1, dataset2);
        plot.mapDatasetToRangeAxis(1, 1);
        
		XYItemRenderer r2 = plot.getRenderer();										// set characteristics for data
		if (r2 instanceof XYLineAndShapeRenderer) {
			XYLineAndShapeRenderer renderer2 = (XYLineAndShapeRenderer) r2;
			renderer2.setBaseShapesVisible(true);										
			renderer2.setShapesVisible(true);											
			renderer2.setShapesFilled(true);
			renderer2.setSeriesPaint(0, Color.black);
			renderer2.setSeriesPaint(1, Color.gray);
			renderer2.setSeriesPaint(2, Color.blue);
			plot.setRenderer(1, renderer2);
		}
		//-------------------------------------------
		ChartFrame frame = new ChartFrame("Distilling", chart);
		frame.pack();
		frame.setVisible(true);
		return chart;
	}

	
	public static void main(String[] args) {	}

}
